<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<!--

-->
<!-- 准备好一个容器-->
<div id="root">
    <!-- 遍历数组 -->
    <h2>人员列表（遍历数组） watch实现过滤</h2>
    <input type="text" placeholder="请输入姓名" v-model="keyWord">
    <ul>
        <li v-for="(p,index) of filterPersons" :key="p.id">
            {{p.name}}-{{p.age}}--{{p.sex}}
        </li>
    </ul>
    <!-- 遍历数组 -->
    <h2>人员列表（遍历数组） compute实现过滤</h2>
    <input type="text" placeholder="请输入姓名" v-model="keyWord2">
    <ul>
        <li v-for="(p,index) of filterPersons2" :key="p.id">
            {{p.name}}-{{p.age}}--{{p.sex}}
        </li>
    </ul>

</div>

<script type="text/javascript">
    Vue.config.productionTip = false

    new Vue({
        el:'#root',
        data:{
            keyWord: '',
            keyWord2: '',
            persons:[
                {id:'001',name:'马冬梅',age:19,sex:'女'},
                {id:'002',name:'周冬雨',age:20,sex:'女'},
                {id:'003',name:'周杰伦',age:21,sex:'男'},
                {id:'004',name:'温兆伦',age:22,sex:'男'}
            ],
            filterPersons: [], // 过滤后的人员列表

        },
        watch: {
            // keyWord(value) {
            //     console.log('value被改变',value)
            //     this.filterPersons = this.persons.filter(p => {
            //         return p.name.indexOf(value) !== -1
            //     })
            // },
            keyWord: {
                immediate: true, // 初始化时让handler调用一下
                handler(value) {
                    console.log('value被改变',value)
                    this.filterPersons = this.persons.filter(p => {
                        return p.name.indexOf(value) !== -1
                    })
                }
            }
        },
        computed: {
            filterPersons2() {
                return this.persons.filter(p => {
                    return p.name.indexOf(this.keyWord2) !== -1
                })
            }
        },
        methods: {

        }
    })
</script>
</body>
</html>